package com.retoc.oawx.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.retoc.oawx.common.util.R;
import com.retoc.oawx.config.shiro.JWTUtil;
import com.retoc.oawx.domain.Ot;
import com.retoc.oawx.domain.req.*;
import com.retoc.oawx.exception.OAException;
import com.retoc.oawx.service.OtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/ot")
@Api("加班模块接口")
public class OtController {
    
    @Autowired
    OtService otService;

    @Autowired
    JWTUtil jwtUtil;

    @PostMapping("/otRequest")
    @ApiOperation("创建用车申请单")
    public R otRequest(@RequestBody @Valid OtReq req, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);

        Ot ot=new Ot();
//        ot.setDriver(req.getDriver());
        ot.setCreateTime(DateUtil.date());
        ot.setCreaterId(userId);
        ot.setUuid(UUID.randomUUID().toString());
        DateTime start = DateUtil.parse(req.getDate()+" "+req.getStartTime()+":00");
        DateTime end = DateUtil.parse(req.getDate()+" "+req.getEndTime()+":00");
        ot.setStartTime(start);
        ot.setEndTime(end);
        ot.setStatus(0);
        ot.setTitle(req.getTitle());
        ot.setReason(req.getReason());
        int row = otService.createOt(ot);
        if (row!=1){
            throw new OAException("创建用车单失败");
        }
        return R.ok();
    }

    @PostMapping("/otUpdate")
    @ApiOperation("创建用车申请单")
    public R otUpdate(@RequestBody @Valid OtReq req, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        Ot ot = otService.selectOtByid(req.getId());
        DateTime start = DateUtil.parse(req.getDate()+" "+req.getStartTime()+":00");
        DateTime end = DateUtil.parse(req.getDate()+" "+req.getEndTime()+":00");
        ot.setStartTime(start);
        ot.setEndTime(end);
        ot.setTitle(req.getTitle());
        ot.setReason(req.getReason());
        int row = otService.updateOt(ot);
        if (row!=1){
            throw new OAException("创建用车单失败");
        }
        return R.ok();
    }

    @PostMapping("/searchOtByCreater")
    @ApiOperation("查询自己的加班申请")
    public R searchOtByCreater(@RequestBody @Valid PageReq req, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        Integer page = req.getPage();
        Integer length = req.getLength();
        Boolean old = req.getOld();
        int start = (page - 1) * length;
        HashMap map = new HashMap<>();
        map.put("createrId", userId);
        map.put("start", start);
        map.put("length", length);
        ArrayList<HashMap> result=new ArrayList<>();
        if (old){
            result=otService.selectOtListByCreator(map);
        }else {
            result=otService.selectOldOtListByCreator(map);
        }
        result.forEach(h->{
            String date = (String) h.get("start");
            String startTime =(String) h.get("startTime");
            String endTime =(String) h.get("endTime");
            DateTime s = DateUtil.parseTime(startTime+":00");
            DateTime e = DateUtil.parseTime(endTime+":00");
            long between = DateUtil.between(s, e, DateUnit.HOUR);
            h.put("hours",between);
        });
        return R.ok().put("result", result);
    }


    @PostMapping("/selectOtById")
    @ApiOperation("根据加班单ID查询加班单信息")
    public R selectOtById(@Valid @RequestBody DeleteReq req){
        HashMap result = otService.selectOtById(req.getId());
        return R.ok().put("result", result);
    }

    @DeleteMapping("/deleteOtById")
    @ApiOperation("根据加班单ID删除加班单")
    public R deleteOtById(@Valid @RequestBody DeleteReq req){
        int i = otService.deleteByid(req.getId());
        return R.ok().put("result","已删除"+i+"条记录");
    }

//    @PostMapping("/otInfo")
//    @ApiOperation("查询用车具体情况")
//    public R otInfo(@Valid @RequestBody SearchMonthCarReq req, @RequestHeader("token") String token){
//        int userId = jwtUtil.getUserId(token);
//        String start = req.getYear().toString() + "-" + req.getMonth().toString() + "-01 00:00:00";
//        DateTime startTime = DateUtil.parseDate(start);
//        DateTime endTime = DateUtil.endOfMonth(startTime);
//        HashMap<String,Object> param=new HashMap<>();
//        param.put("startTime", startTime);
//        param.put("endTime", endTime);
//        param.put("carId", req.getCarId());
//        ArrayList<HashMap> result= otService.searchOtInfo(param);
//        Materials car = materialsMapper.selectByid(req.getCarId());
//        int unused=0;
//        int used=0;
//        for (HashMap h:result
//        ) {
//            if (h.get("type").equals("已用")){
//                used++;
//            } else if(h.get("type").equals("未用")){
//                unused++;
//            }
//        }
//        HashMap info=new HashMap<>();
//        info.put("used", used);
//        info.put("unused",unused);
//        return R.ok().put("result",result).put("car",car).put("info",info);
//    }
}
